package com.ts.crawler.selenium;

import com.ts.crawler.pipeline.FlightInfoPipeline;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.remote.RemoteWebDriver;
import us.codecraft.webmagic.*;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FlightCtripProcesser implements PageProcessor {

    private RemoteWebDriver driver;

    private static final String targetUrl ="https://flights.ctrip.com/international/search/domestic";

    private Site site = Site.me().setSleepTime(1000).setTimeOut(10000).setCharset("utf-8")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");

    public void process(Page page) {
        Html html = page.getHtml();

        List<Selectable> nodes = html.css(".flight-list > span > div").nodes();
//        System.out.println(nodes.toString());
        System.out.println(nodes.size());
        int count=0;
        for (Selectable node : nodes) {
            Map<String,String> map = new HashMap();
            StringBuilder sb = new StringBuilder();
            String airlineName = node.$(".flight-row").xpath("///div[@class='flight-airline']/div[@class='airline-name']/span/text()").toString();
            if(airlineName == null){
                continue;
            }

            String departTime = node.$(".flight-row").xpath("///div[@class='flight-detail']/div[@class='depart-box']/div[@class='time']/text()").toString();
            String arriveTime = node.$(".flight-row").xpath("///div[@class='flight-detail']/div[@class='arrive-box']/div[@class='time']/text()").toString();
//            List<Selectable> planes = node.$(".flight-row").xpath("///div[@class='flight-airline']/div[@class='plane']/div[@class='plane']").nodes();
//            for(Selectable p : planes){
//                String plane = p.xpath("///span[@class='plane-No']/span[@class=' ']/text()").toString();
//                System.out.println("机型"+plane);
//                System.out.println(p);
//            }

            String departAirPort = node.$(".flight-row").xpath("///div[@class='flight-detail']/div[@class='depart-box']/div[@class='airport']/span/text()").toString();
            String arriveAirPort = node.$(".flight-row").xpath("///div[@class='flight-detail']/div[@class='arrive-box']/div[@class='airport']/span/text()").toString();
            String totalTime = node.$(".flight-row").xpath("///div[@class='flight-consume']/text()").toString();
            List<Selectable> cabins = node.$(".flight-seats").xpath("///div[@class='seat-row']").nodes();
//            System.out.println(cabins.size());
//            System.out.println(cabins);
            int i = 1;
            for(Selectable c : cabins){
                String name = c.$(".seat-type").xpath("///span/text()").toString();
                String price = c.$(".seat-operate").xpath("//div[@class='price']").regex("\\>[0-9]+").toString();
                price = price.substring(1);
                sb.append(name+price);
//                System.out.println("舱位：" + name);
//                System.out.println("价格：" + price);

                map.put("舱位"+ i,sb.toString());
                ++i;
            }
            i=1;

//            System.out.println("航班：" + airlineName);
//            System.out.println("起飞时间：" + departTime);
//            System.out.println("到达时间：" + arriveTime);
//            System.out.println("起飞机场：" + departAirPort);
//            System.out.println("到达机场：" + arriveAirPort);
//            System.out.println("飞行时间：" + totalTime);
//            System.out.println("----------------------");

            map.put("航班",airlineName);
            map.put("起飞时间",departTime);
            map.put("到达时间",arriveTime);
            map.put("起飞机场",departAirPort);
            map.put("到达机场",arriveAirPort);
            map.put("飞行时间",totalTime);
            ++count;
            page.putField("result"+count,map);

        }

    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Map<String,String> params =new HashMap<String, String>();
        //公共参数
        String startCity = "长沙(CSX)";
        String destCity = "北京(BJS)";
        String startDate = "2021-10-28";
        String endDate ="2021-10-30";
        String withChild ="true";
        String withInfant ="true";
        //Y_S C_F
        String cabin ="C_F";
        //国内/国际机票
        String flightWay ="S";
        //单程 往返
        String flightType ="D";

        params.put("startCity",startCity);
        params.put("destCity",destCity);
        params.put("startDate",startDate);
        params.put("endDate",endDate);
        params.put("withChild",withChild);
        params.put("withInfant",withInfant);
        params.put("cabin",cabin);
        params.put("flightWay",flightWay);
        params.put("flightType",flightType);

        Spider.create(new FlightCtripProcesser())
                .addPipeline(new FlightInfoPipeline("E:\\static\\"))
//                .addPipeline(new FilePipeline())
                .addPipeline(new ConsolePipeline())
                .setDownloader(new FlightCtripDownLoader(params))
                .addUrl("https://flights.ctrip.com/international/search/domestic")
                .thread(1)
                .run();
    }
}
